<?php
namespace app\index\service;
use think\Db;
use think\Loader;
class  Clonsul{


    //咨询提问
    function  consultQuizApi($consult_type_id,$ct_title,$details){
     try{
        if(empty($consult_type_id) || empty($ct_title) || empty($details)){
            return messageInfo('101', '咨询类型、标题与咨询的问题必需填写');
        }
        $lt_user = session('lt_user');
        $date    = date('Y-m-d H:i:s');
        //开启事务
        $consult_push    = Db::name('consult_push');
        $consult_push->startTrans();

        //写入后台推送表  随机后台律师
        $data_sie  = Db::name('consult_push_sie')->field('admin_id')->select();

        if(!empty($data_sie)){
            foreach($data_sie as $k=>$v){
                $array_rand[] = $v['admin_id'];
            }
            $key      = array_rand($array_rand);
            $admin_id = $array_rand[$key];
            $admin_phone   = Db::name('admin')->where('admin_id',$admin_id)->value('a_phone');
        }else{
            return messageInfo('101', '咨询律师已下线！');
        }
        //定入主表
        $data_aray = array(
          'consult_type_id' => $consult_type_id,
          'u_uid'           => $lt_user['u_uid'],
          'ct_details'      => $details,
          'ct_title'        => $ct_title,
          'ct_addtime'      => $date
        );
        $ad       = Db::name('consult')->insert($data_aray);
        if(!$ad){
            $consult_push->rollback();
            return messageInfo('101','咨询失败3');
        }
        $consult_id = Db::name('consult')->getLastInsID();

         if($admin_phone){
             //写入记录表
             $data_array_log = array(
                 'pl_phone'   => $admin_phone,
                 'pl_details' => $details,
                 'pl_addtime' => $date
             );
             $b = Db::name('push_log')->insert($data_array_log);
             if(!$b){
                 $consult_push->rollback();
                 return messageInfo('101','咨询失败3');
             }
         }
        $data_push_array  = array(
            'admin_id'       => $admin_id,
            'u_uid'           => $lt_user['u_uid'],
            'consult_id'     => $consult_id,
            'cp_addtims'     => $date
        );
        $o = $consult_push->insert($data_push_array);
        if(!$o){
            $consult_push->rollback();
            return messageInfo ( '101', '咨询失败！' );
        }
        $consult_push->commit();
         //发送短信
        $this->dataPush($admin_phone,$lt_user,$details,1);
        return messageInfo ( '202', '咨询成功！' );
     } catch (\Exception $e) {
            $response = messageInfo ( '101', '服务器错误！' );
     }
        return $response;
    }

    //咨询回复
    function counsultReplyApi($consult_id,$cr_state,$details){
        try{
            if(empty($consult_id) || empty($cr_state)){
                return messageInfo('101', '系统参数错误!');
            }
            if(empty($details)){
                return messageInfo('101', '回复内容必需填写!');
            }
            $lt_user = session('lt_user');
         /*   $uid     = Db::name('consult')->where('consult_id',$consult_id)->value('u_uid');
            if($uid != $lt_user['u_uid']){
                return messageInfo('101', '你没有回复权限!');
            }*/
            $admin_id = Db::name('consult_push')->where('consult_id',$consult_id)->value('admin_id');
          /*  if(count($admin_id) <= 0 || count($admin_id) == 1){
                return messageInfo('101', '此咨询，律师还未回复处理，请勿重复提交!');
            }*/
            /*$admin_phone   = Db::name('admin')->where('admin_id',$admin_id)->value('a_phone');*/

            $date    = date('Y-m-d H:i:s');
            //开启事务
            $consult_push    = Db::name('consult_push');
            $consult_push->startTrans();
            //写入咨询副表
            $data_array = array(
                'consult_id' => $consult_id,
                'u_uid'      => $lt_user['u_uid'],
                'cr_type'    => 1,
                'cr_state'   => $cr_state,
                'cr_details' => $details,
                'cr_addtims' => $date,
            );
            $consult_reply = Db::name('consult_reply');
            $c = Db::name('consult_reply')->insert($data_array);
            if(!$c){
                $consult_push->rollback();
                return messageInfo('101','回复失败4!');
            }
            //写入回复总数
            $p = Db::name('consult')->where('consult_id',$consult_id)->setInc('ct_count',1);
            if(!$p){
                $consult_push->rollback();
                return messageInfo('101','回复失败!');
            }
          /*  if($admin_phone){
                //写入记录表
                $data_array_log = array(
                    'pl_phone'   => $admin_phone,
                    'pl_details' => $details,
                    'pl_addtime' => $date
                );
                $b = Db::name('push_log')->insert($data_array_log);
                if(!$b){
                    $consult_push->rollback();
                    return messageInfo('101','回复失败');
                }
                //发送短信
                $res = $this->dataPush($admin_phone,$details,2);
                if($res['code'] == '101'){
                    $consult_push->rollback();
                    return $res;
                }
            }*/
            $consult_reply_id = $consult_reply->getLastInsID();
            $data_push_array  = array(
                'admin_id'       => $admin_id,
                'u_uid'          => $lt_user['u_uid'],
                'consult_id'     => $consult_id,
                'consult_reply_id' => $consult_reply_id,
                'cp_addtims'     => $date
            );
            $o = $consult_push->insert($data_push_array);
            if(!$o){
                $consult_push->rollback();
                return messageInfo ( '101', '回复失败！' );
            }
            $consult_push->commit();
            return messageInfo ( '202', '回复成功！' );
        } catch (\Exception $e) {
            $response = messageInfo ( '101', '服务器错误！' );
        }
        return $response;
    }



    //短信推送
    function dataPush($phone,$lt_user,$details,$type=1){
        $arrt['up_phone'] = $phone;
        if ($type == 1) {
            $arrt['product']  = '用户咨询';
            $arrt['succeed']  = '咨询成功';
            $arrt['errot']    = '咨询失败';
        }else{
            $arrt['product']  = '用户回复';
            $arrt['succeed']  = '咨询成功';
            $arrt['errot']    = '回复失败';
        }
       /* // 短信内容参数
        Loader::import('Alidayu.AlidayuClient', EXTEND_PATH, '.class.php');
        Loader::import('Alidayu.Request.SmsNumSend', EXTEND_PATH, '.class.php');
        $client  = new \Alidayu\AlidayuClient();
        $request = new \Alidayu\Request\SmsNumSend ();
        $smsParams = array ('name' => $arrt['up_phone'],'details' => $details);
        // 设置请求参数
        $req  = $request->setSmsTemplateCode (config('AlidayuPush'))->setRecNum ( $arrt['up_phone'] )->setSmsParam ( json_encode ( $smsParams ) )->setSmsFreeSignName (config('AlidayuName'))->setSmsType ( 'normal' )->setExtend ( 'demo' );
        $data = $client->execute ( $req );
        if(!array_key_exists('alibaba_aliqin_fc_sms_num_send_response',$data) ){
            return messageInfo ( '101', $arrt['errot'] );
        }*/
        Loader::import('Aldy.Sms', EXTEND_PATH, '.class.php');
        $cofig ['templateCode'] = config('AlidayuID_TZ');
        $sms = new \Sms($cofig);
        $smsParams = array ('name' => $arrt['up_phone'],'user'=>$lt_user['u_phone'],'details'=>$details);
        $data = $sms->send_verify($arrt['up_phone'], json_encode ( $smsParams ));
        if ($data['Code'] == 'OK' && $data['Message'] == 'OK') {
            $response = messageInfo ( '202', $arrt['succeed'] );
        } else {
            $response = messageInfo ( '101', $arrt['errot'] );
        }
        return $response;
    }


    //咨询收藏 -新闻收藏
    function counsultCollectApi($ccc_id,$ucc_state=1){
        if(empty($ccc_id)){
            return messageInfo('101', '系统参数错误!');
        }
        $user_collect = Db::name('user_collect');
        $consult      = Db::name('consult');
        $user_collect->startTrans();
        $lt_user = session('lt_user');
        $where['ucc_id']      = $ccc_id;
        $where['u_uid']       = $lt_user['u_uid'];
        $where['ucc_state']   = $ucc_state;
        $user_collect_id      = $user_collect->where($where)->value('user_collect_id');
        if(count($user_collect_id) >= 1){
            $return_name = '取消';
            $b = $consult->where('consult_id',$ccc_id)->setDec('ct_sum');
            if(!$b){
                $user_collect->rollback();
                return messageInfo('101', $return_name.'失败!');
            }
            $a = $user_collect->where('user_collect_id',$user_collect_id)->delete();
        }else{
            $return_name = '收藏';
            $b = $consult->where('consult_id',$ccc_id)->setInc('ct_sum');
            if(!$b){
                $user_collect->rollback();
                return messageInfo('101', $return_name.'失败!');
            }
            $data_array = array(
                'ucc_id'       => $ccc_id,
                'ucc_state'    => $ucc_state,
                'u_uid'        => $lt_user['u_uid'],
                'ccc_addtime'  => date('Y-m-d H:i:s'),
            );
            $a = $user_collect->insert($data_array);
        }
        if(!$a){
            $user_collect->rollback();
            return messageInfo('101', $return_name.'失败!');
        }
        $user_collect->commit();
        return messageInfo('202', $return_name.'成功!');
    }

    //新闻评论
    function newsCommentApi($news_id,$content){
        if(empty($news_id)){
            return messageInfo('101', '系统参数错误!');
        }
        if(empty($content)){
            return messageInfo('101', '评论不能为空!');
        }
        $lt_user = session('lt_user');
        $data_array = array(
            'news_id'     => $news_id,
            'nc_content'  => $content,
            'u_uid'       => $lt_user['u_uid'],
            'nc_addtime'  => date('Y-m-d H:i:s'),
        );
        $a = Db::name('news_comment')->insert($data_array);
        if(!$a){
            return messageInfo('101', '评论失败!');
        }
        return messageInfo('202', '评论成功!');
    }

}
